/* 
 * @(#)SmsClient.java    Created on 2011-7-10
 * Copyright (c) 2011 ZDSoft Networks, Inc. All rights reserved.
 * $Id$
 */
package bjxg.test.msgClient;

import net.zdsoft.keel.util.DateUtils;
import net.zdsoft.keel.util.StringUtils;
import net.zdsoft.message.client.MessageClient;
import net.zdsoft.message.client.MessageException;
import net.zdsoft.message.client.MessageHandler;
import net.zdsoft.smsg.sms.msg.MoMsg;
import net.zdsoft.smsg.sms.msg.MtMsg;
import net.zdsoft.smsg.sms.msg.ReportMsg;
import net.zdsoft.smsg.sms.msg.ResultMsg;

/**
 * @author Administrator
 * @version $Revision: 1.0 $, $Date: 2011-7-10 下午02:12:21 $
 */
public class SmsClient extends Thread implements MessageHandler {
    public MessageClient messageClient;
    public int resultCount;
    public int reportCount;
    public int moCount;

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        SmsClient client = new SmsClient();
        client.start();
        try {
            Thread.sleep(1000);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        String content = "测测测测测测测测试测试测试测试测试测试测试测试测试测试测试测试试试试试试试试70字测试测试测试测试测试测试测试测试测试测试试试试试试试试70字测试测试测试测试测试测试测试测试测试测试试试试试试试试70字";
        for (int n = 1; n <= 1 * 10000; n++) {
            String phone = "156" + StringUtils.enoughZero(n + "", 8);
            String mtId = StringUtils.enoughZero(DateUtils.currentDate2String() + n + "", 32);
            MtMsg mt = new MtMsg();
            mt.setContent(content);
            mt.setLongSms(false);
            mt.setMtId(mtId);
            mt.setNeedReport(true);
            mt.setPhone(phone);
            mt.setSequence(mt.getMtId());
            mt.setServiceCode("servicecod");
            mt.setSpNumber("10657061");

            client.messageClient.sendMessage("sp_sxyd", mt.encode());
            Thread.sleep(100);
            if ((n % 10) == 0) {
                Thread.sleep(30);
                // System.out.println(n + "--" + msg);
            }
        }

        while (true) {
            try {

                System.out.println("resultCount=" + client.resultCount);
                System.out.println("reportCount=" + client.reportCount);
                System.out.println("moCount=" + client.moCount);
                Thread.sleep(10 * 1000);
            }
            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void start() {
        try {
            messageClient = new MessageClient(this);
            messageClient.connect("211.142.5.227", 5100, "etoh2_web_sms", "etoh2_web_sms");
        }
        catch (MessageException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void kickedOutByServer() {
        // TODO Auto-generated method stub

    }

    @Override
    public void messageReceived(byte[] arg0, String arg1, String msg) {
        // System.out.println(arg2);
        if (ResultMsg.isThis(msg)) {
            resultCount++;
        }
        else if (ReportMsg.isThis(msg)) {
            reportCount++;
        }
        else if (MoMsg.isThis(msg)) {
            moCount++;
        }
    }

    @Override
    public void messageSubmitted(byte[] arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (true) {
            try {

                System.out.println("resultCount=" + resultCount);
                System.out.println("reportCount=" + reportCount);
                System.out.println("moCount=" + moCount);
                Thread.sleep(500 * 1000);
            }
            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
